﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Rose.Helper;
using Rose.Models;

namespace Rose.Controllers
{
    [HandleError]
    public class HomeController : Controller
    {
        private Database_RoseEntities db = new Database_RoseEntities();
        private RecommendHelpModel recommendhelpmodel = new RecommendHelpModel();

        public ActionResult Index()
        {
            
            ViewData["PageTitle"] = "School Party -- 大学生的派对";
            return View();
        }

     


        [Authorize]
        
        public ActionResult RecommendJson(int currentpage, int type=1, bool isOppGender=true, bool isDescending=true)
        {
            const int PAGESIZE = 2;
            // tongxiao 1
            //  benzhuanye 2
            //  laoxiang  3
            string selfId = new CheckHelpModel().CheckCurrentUserId();
            int allCount = 0;
            List<RecommendModel> recommendList = new List<RecommendModel>();
            
            switch (type)
            {
                
                case 2:

                    allCount = recommendhelpmodel.GetMajorFriendsCount(selfId, isOppGender);
                    recommendList = recommendhelpmodel.GetMajorRecommendFriend(selfId, isOppGender, isDescending, currentpage, PAGESIZE);
                    break;
                
                   
                case 3:
                    allCount = recommendhelpmodel.GetAddressFriendsCount(selfId, isOppGender);
                    recommendList = recommendhelpmodel.GetAddressRecommendFriend(selfId, isOppGender, isDescending, currentpage, PAGESIZE);
                    break;

                default: allCount = recommendhelpmodel.GetUniversityFriendsCount(selfId, isOppGender);
                    recommendList = recommendhelpmodel.GetUniversityRecommendFriend(selfId, isOppGender, isDescending, currentpage, PAGESIZE);
                    break;
            }


            if (recommendList != null)
                return Json(recommendList, JsonRequestBehavior.AllowGet);
            return Content("错误");
        }


        [Authorize]
        public ActionResult RecommendPart(int currentpage,string type="university",bool isOppGender=false,bool isDescending=true)
        {
            const int PAGESIZE = 2;
            
            string selfId = new CheckHelpModel().CheckCurrentUserId();
            int allCount = 0;
            List<RecommendModel> recommendList = new List<RecommendModel>();
            string lowertype=type.ToLower();
            switch (lowertype)
            { 
                case "samemajor":

                    allCount = recommendhelpmodel.GetMajorFriendsCount(selfId, isOppGender);
                    recommendList = recommendhelpmodel.GetMajorRecommendFriend(selfId, isOppGender, isDescending, currentpage, PAGESIZE);
                    break;
                case "university":
                    allCount = recommendhelpmodel.GetUniversityFriendsCount(selfId, isOppGender);
                    recommendList = recommendhelpmodel.GetUniversityRecommendFriend(selfId, isOppGender, isDescending, currentpage, PAGESIZE);
                    break;
                case "address":
                    allCount = recommendhelpmodel.GetAddressFriendsCount(selfId, isOppGender);
                    recommendList = recommendhelpmodel.GetAddressRecommendFriend(selfId, isOppGender, isDescending, currentpage, PAGESIZE);
                    break;
                default:
                    allCount = recommendhelpmodel.GetMajorFriendsCount(selfId, lowertype, isOppGender);
                    recommendList = recommendhelpmodel.GetMajorRecommendFriend(selfId, lowertype, isOppGender, isDescending, currentpage, PAGESIZE);
                    break;
            }


            if (recommendList != null)
                return PartialView(recommendList);
            return Content("错误");
        }



        [Authorize]
        public ActionResult HotPart()
        {
            return View();
        }

        [Authorize]
        public ActionResult BirthPart()
        {
            string selfId=new  CheckHelpModel().CheckCurrentUserId();
            //假设五天的生日知道
            const int SPAN = 5;
            var returnModel =new NewTrendHelpModel().GetFriendsByBirth(selfId, SPAN);
            return View(returnModel);
        }
        [Authorize]
        public ActionResult DiaryPart()
        {
            return View();
        }



       


       
        [Authorize]
        public ActionResult TrendPart()
        {
            string selfId = new CheckHelpModel().CheckCurrentUserId();
            const int PAGESIZE = 10;
            var returnModel = new NewTrendHelpModel().GetFriendsAndFocusTrendList(selfId, PAGESIZE);
            return View(returnModel);
        }

        [Authorize]
        public ActionResult MoreTrendJson(string lastTime)
        {

            //这里需要先判断一下接收到的字符串类型
            //2011/8/27 14:05:51

            string[] date = lastTime.Split(' ');
            string[] first = date[0].Split('/');
            string[] second = date[1].Split(':');
            int year = int.Parse(first[0]);
            int month = int.Parse(first[1]);
            int day = int.Parse(first[2]);
            int hour = int.Parse(second[0]);
            int minute = int.Parse(second[1]);
            int miao = int.Parse(second[2]);
            DateTime time = new DateTime(year, month, day, hour, minute, miao);



            string selfId = new CheckHelpModel().CheckCurrentUserId();
            const int MORE = 10;

            var returnModel = new NewTrendHelpModel().GetFriendsAndFocusMoreTrendList(selfId, time, MORE);
            return Json(returnModel, JsonRequestBehavior.AllowGet);


        }

        [Authorize]
        public ActionResult FocusFeelingPart()
        {

            string selfId = new CheckHelpModel().CheckCurrentUserId();
            const int PAGESIZE = 1;
            var returnModel = new NewTrendHelpModel().GetFocusesTrendList(selfId, PAGESIZE).FirstOrDefault();
            if (returnModel==null)
                return View(new NewTrendModel() { Type=100 });
            return View(returnModel);
           
        }


        [Authorize]
        public ActionResult MoreFocusFeelingJson(string lastTime)
        {
            string[] date = lastTime.Split(' ');
            string[] first = date[0].Split('/');
            string[] second = date[1].Split(':');
            int year = int.Parse(first[0]);
            int month = int.Parse(first[1]);
            int day = int.Parse(first[2]);
            int hour = int.Parse(second[0]);
            int minute = int.Parse(second[1]);
            int miao = int.Parse(second[2]);
            DateTime time = new DateTime(year, month, day, hour, minute, miao);
             
            string selfId = new CheckHelpModel().CheckCurrentUserId();
            const int MORE = 5;
            var returnModel = new NewTrendHelpModel().GetFocusesMoreTrendList(selfId,time, MORE);
            return Json(returnModel, JsonRequestBehavior.AllowGet);   
        }

        [Authorize]
        public ActionResult SelfPart(string friendId="")
        {
            string selfId=new CheckHelpModel().CheckCurrentUserId();

            if (!string.IsNullOrEmpty(friendId))
                selfId = friendId;


            UserInfo selfInfo = new CheckHelpModel().CheckUserInfo(selfId);
            SelfModel model = new SelfModel()
            {
                UserId = selfInfo.Id,
                Name = selfInfo.Name,
                ImagePath=selfInfo.ImagePath,
                FocusCount = selfInfo.FocusCount,
                FriendCount = selfInfo.FriendCount,
                Score = selfInfo.Score,
                RequestCount = db.RequestFriends.Where(c=>c.TargetId==selfId &&c.IsResponsed==false).Count()
            };
            return PartialView(model);

        }

        public ActionResult HomeScriptPart()
        {
            return View();
        }

        public ActionResult AlbumScriptPart()
        {
            return View();
        }

        public ActionResult SingleTrendPart(NewTrendModel model)
        {
            return View(model);
        }

        public ActionResult SingleRequestPart(RequestModel model)
        {
            return View(model);
        }

        public ActionResult SingleRecommendPart(RecommendModel model)
        {
            return View(model);
        }

        public ActionResult SingleDealRequestPart(RequestModel model)
        {
            return View(model);
        }

        public ActionResult RightPart()
        {
            return View();
        }

    }
}
